package com.google.code.sms.util;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;

import com.google.code.sms.Logger;

/**
 * Miscellaneous file utility methods.
 *
 * @author Scott Ware
 */
public final class FileUtil {

    private static final Logger LOG = Logger.getLogger("FileUtil");
    
    /**
     * Returns whether the given file is hidden.
     *
     * @param file The file in question.
     * @return Whether the file is hidden.
     */
    public static boolean isHidden(File file) 
    {
        // Hidden files start with "."
        String name = file.getName();
        return name.startsWith(".");
    }
    
    /**
     * Returns a short path for the given file. The path consists of the name of
     * the parent directory and the given file.
     */
    public static String getShortPath(File file) 
    {
        if (file == null) {
            return null;
        }
        
        File parent = file.getParentFile();
        
        if (parent == null) 
        {
            return file.getName();
        }
        
        return parent.getName() + File.separator + file.getName();
    }

    /**
     * Closes the "closable", ignoring any exceptions.
     *
     * @param closeable The Closable to close, may be {@code null}.
     */
    public static void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                // Ignored
            }
        }
    }
}